package com.jxsd.edu.cn.struts2.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.jxsd.edu.cn.struts2.models.Category;
import com.jxsd.edu.cn.struts2.models.PubMsgview;
import com.jxsd.edu.cn.struts2.models.Topic;
import com.jxsd.edu.cn.struts2.serviceImpl.PubMsgServiceImpl;
import com.jxsd.edu.cn.struts2.serviceImpl.TopicServiceImpl;
import com.jxsd.edu.cn.struts2.services.PubMsgService;
import com.jxsd.edu.cn.struts2.services.TopicService;
import com.jxsd.edu.cn.tools.Page;
import com.jxsd.edu.cn.tools.PageBean;
import com.jxsd.edu.cn.tools.StringTool;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TopicAcion extends  ActionSupport
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4723835306781521012L;
	/**
	 * 一级类别信息
	 * */
	private     List<Category>   list;
    /**
     * 获取一个城市某个二级类别帖子页面数据(按时间排序)
     * */
	private     Page<Topic>   page;
	/**
	 * topic服务
	 * */
	private   TopicService  service=new   TopicServiceImpl();
	/**
	 * 分页请求器
	 * */
	private   PageBean<Topic>  pagebean=new  PageBean<Topic>(10);
	/**
	 * 二级类别编号
	 * */
	private   String  smallId;
	/**
	 * 最新推荐
	 * */
	private   List<PubMsgview>  msgs;
	/**
	 * 获取同城的最新的前10条帖子
	 * */
	private  List<Topic>  topics;
	
	/**
	 * 帖子记录
	 * */
	private   Topic   topic=new  Topic();
	
	//文件
 	private     File  image;
    private   String  imageContentType;
    private   String  imageFileName;
    private   String  savePath;
	
	/**
	 * PubMsgService
	 * */
	private   PubMsgService  pubService=new  PubMsgServiceImpl();
	
	/**
	 * 获取一个城市某个二级类别帖子页面数据(按时间排序)
	 * */
	public  String  cityPages()
	{
		//获取一级类别信息
		list=service.getAllCatagory();
		String  cityName=null;
		cityName=(String)ActionContext.getContext().getSession().get("cityName");//从session中获取客户访问的城市名
		
		if(null!=page)
		{
			pagebean.setCurrentPage(page.getCurrent_page());
		}
		
		pagebean.setPageSize(10);
		page=service.getPageTopics(pagebean, smallId, cityName);
		
		
		
		msgs=pubService.getPubMsgs(smallId, cityName);
		
		
		return  SUCCESS;
	}
	/**
	 * 获取某个二级类别所有数据
	 * */
	public  String  SmallPages()
	{
		//获取一级类别信息
		list=service.getAllCatagory();
		page=service.getAllTopices(pagebean, smallId);
		return  SUCCESS;
	}
	
	/**
	 * 获取一条具体的帖子包括回复
	 * */
	public  String  queryTopic()
	{
		//获取一级类别信息
		list=service.getAllCatagory();
		//获取帖子信息
		topic=service.getTopic(Integer.toString(topic.getTopicID()));
		
		
		
		String  cityName=null;
		cityName=(String)ActionContext.getContext().getSession().get("cityName");//从session中获取客户访问的城市名
		
		//获取同城最新的前10条帖子
		queryNewlyTopic(cityName);
		//获取同城最新的前10条物品发布记录
		queryNewlyMsgs(cityName);
		
		return "detail";
	}
	/**
	 * 添加一条帖子
	 * @throws Exception 
	 * */
	public  String  addTopic() throws Exception
	{
		int  userId;
		//从session中获取客户访问的城市名
		userId=(Integer)ActionContext.getContext().getSession().get("userId");
		topic.setUserID(userId);
		topic.setPostTime(StringTool.getCurDate());
		
		String  picture=doUpload();
		topic.setContentImage(picture);
		
		
		
		System.out.println("**********************************8");
		System.out.println(topic.getUserID());
		System.out.println(topic.getContent());
		System.out.println(topic.getPostTime());
		System.out.println(topic.getTitle());
		System.out.println(topic.getSmallName());
		System.out.println("**********************************8");
		
		
		service.addTopic(topic);
		return  "detele";
	}
	
	/**
	 * 删除一条帖子
	 * */
	public  String  deleteTopic()
	{
		service.deleteTopic(Integer.toString(topic.getTopicID()));
		return  "detele";
	}
	
	 /**
	    * hander  the  upload  file
	 * @throws FileNotFoundException 
	    * */
	   private  String  doUpload() throws Exception
	   {
		   //无图
		   if(null==image)
		   {
			   return  null;
		   }
		   
		   String  fosPath=null;
		// 对于上传文件的存放地址来建立一个输出流
			fosPath=getSavePath()+"/"+ System.currentTimeMillis()
				+ imageFileName.substring(imageFileName.lastIndexOf("."),
						imageFileName.length());
			
			 
	        String path = ServletActionContext.getServletContext()     
	                .getRealPath("/")+fosPath;
			
		   FileOutputStream fos = new FileOutputStream(path);
		   FileInputStream fis = new FileInputStream(getImage());
		   byte[] buffer = new byte[1024];
		   int len = 0;
		   while ((len = fis.read(buffer)) > 0)
		   {
		    fos.write(buffer , 0 , len);
		   }
		   
		   fis.close();
		   fos.close();
		   
		   return  fosPath;
	   }
	
	/**
	 * 获取同城最新的前10条物品信息发布记录
	 * */
	private   void   queryNewlyMsgs(String  cityName)
	{
		msgs=pubService.queryNewlyMsgs(cityName);
	}
	/**
	 * 获取同城最新的前10条帖子
	 * */
	private   void  queryNewlyTopic(String  cityName)
	{
		topics=service.queryNewlyTopic(cityName);
	}
	
	public List<Category> getList() {
		return list;
	}

	public void setList(List<Category> list) {
		this.list = list;
	}

	public Page<Topic> getPage() {
		return page;
	}

	public void setPage(Page<Topic> page) {
		this.page = page;
	}
	public PageBean<Topic> getPagebean() {
		return pagebean;
	}
	public void setPagebean(PageBean<Topic> pagebean) {
		this.pagebean = pagebean;
	}
	
	public  void  setSmallId(String  smallId)
	{
		this.smallId=smallId;
	}
	public  String  getSmallId()
	{
		return  smallId;
	}
	public List<PubMsgview> getMsgs() {
		return msgs;
	}
	public void setMsgs(List<PubMsgview> msgs) {
		this.msgs = msgs;
	}
	public Topic getTopic() {
		return topic;
	}
	public void setTopic(Topic topic) {
		this.topic = topic;
	}
	public List<Topic> getTopics() {
		return topics;
	}
	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}
	public File getImage() {
		return image;
	}
	public void setImage(File image) {
		this.image = image;
	}
	public String getImageContentType() {
		return imageContentType;
	}
	public void setImageContentType(String imageContentType) {
		this.imageContentType = imageContentType;
	}
	public String getImageFileName() {
		return imageFileName;
	}
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}
	public String getSavePath() {
		return savePath;
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
}
